package com.sanjiahantang.youjiaclient.module.me.require.views.impl;

import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.support.v4.widget.SwipeRefreshLayout;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.sanjiahantang.uilibrary.utils.Utils;
import com.sanjiahantang.youjiaclient.R;
import com.sanjiahantang.youjiaclient.base.BaseMvpActivity;
import com.sanjiahantang.youjiaclient.module.me.order.beans.RequireOrderBean;
import com.sanjiahantang.youjiaclient.module.me.order.views.impl.OrderConfirmActivity;
import com.sanjiahantang.youjiaclient.module.me.require.beans.RequireListBean;
import com.sanjiahantang.youjiaclient.module.me.require.presenters.RequirePresenter;
import com.sanjiahantang.youjiaclient.module.me.require.views.RequireView;

import static com.sanjiahantang.youjiaclient.config.StaticValue.KEY_PARAM;

/**
 * Created by zy on 2017/10/24.
 */

public class RequireDetailActivity extends BaseMvpActivity<RequireView, RequirePresenter> implements RequireView, View.OnClickListener {

    private RequireListBean.Item data;
    private ImageView ivPhone;
    private int requireId;
    private SwipeRefreshLayout refreshLayout;

    @Override
    public void showProgress() {
        super.showProgress($this);
    }

    @Override
    public RequirePresenter initPresenter() {
        return new RequirePresenter();
    }

    private TextView btnGo;

    @Override
    public void initView() {
        setContentView(R.layout.activity_require);
        TextView title = (TextView) findViewById(R.id.title);
        refreshLayout = (SwipeRefreshLayout) findViewById(R.id.refreshLayout);
        title.setText("装修需求单");
        findViewById(R.id.fl_Left).setOnClickListener(this);
        btnGo = (TextView) findViewById(R.id.btnGo);
        requireId = getIntent().getIntExtra(KEY_ID,0);
        if (requireId == 0){
            Utils.showToast($this,"需求单不见了");
            finish();
            return;
        }
        data = (RequireListBean.Item) getIntent().getSerializableExtra(KEY_PARAM);
        ivPhone = (ImageView) findViewById(R.id.ivPhone);
        ivPhone.setOnClickListener(this);
        refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                onResume();
            }
        });
    }

    @Override
    public void initData() {

    }

    @Override
    protected void onResume() {
        super.onResume();

        presenter.findUserRequireByIdPhone($this, requireId, new OnLoadListener() {
            @Override
            public void onLoad(Object content) {
                if (refreshLayout != null){
                    refreshLayout.setRefreshing(false);
                }
                if (content != null && content instanceof RequireListBean.Item){
                    data = (RequireListBean.Item) content;

                    TextView txtName = (TextView) findViewById(R.id.txtName);
                    txtName.setText(data.getUserName());
                    TextView txtIdCode = (TextView) findViewById(R.id.txtIdCode);
                    if (!TextUtils.isEmpty(data.getIdCode())) {
                        txtIdCode.setText(data.getIdCode());
                    }

                    TextView txtSex = (TextView) findViewById(R.id.txtSex);
                    txtSex.setText("1".equals(data.getSex()) ? "女" : "男");
                    TextView txtJob = (TextView) findViewById(R.id.txtJob);
                    txtJob.setText(data.getJobName());
                    TextView txtCity = (TextView) findViewById(R.id.txtCity);
                    txtCity.setText(data.getAddress());
                    TextView txtAddress = (TextView) findViewById(R.id.txtAddress);
                    txtAddress.setText(data.getApartmentName());
                    TextView txtBaby = (TextView) findViewById(R.id.txtBaby);
                    txtBaby.setText(data.getIsHaveBaby() == 0 ? "否" : "是");
                    TextView txtOld = (TextView) findViewById(R.id.txtOld);
                    txtOld.setText(data.getHasTogether() == 0 ? "否" : "是");
                    TextView txtRoomType = (TextView) findViewById(R.id.txtDoor);
                    txtRoomType.setText(data.getApartmentTypeName());
                    TextView txtStyle = (TextView) findViewById(R.id.txtStyle);
                    txtStyle.setText(data.getLikeStyleName());
                    TextView txtArea = (TextView) findViewById(R.id.txtArea);
                    txtArea.setText(data.getApartmentArea());
                    TextView txtPrice = (TextView) findViewById(R.id.txtPrice);
                    txtPrice.setText(data.getBudgetName());
                    TextView txtPreTime = (TextView) findViewById(R.id.txtPreTime);
                    txtPreTime.setText(data.getPlanDecorateTime());

                    presenter.getRequireOrderPrice($this, data.getId(), new OnLoadListener() {
                        @Override
                        public void onLoad(final Object content) {
                            final RequireOrderBean.Data orderData = (RequireOrderBean.Data) content;
                            ivPhone.setImageResource(R.drawable.i_phone_green_strike);
                            ivPhone.setBackgroundResource(R.drawable.x_shape_gray_border_r20);
                            btnGo.setBackgroundResource(R.drawable.x_shape_green);
                            btnGo.setTextSize(14);
                            if (data.getIsDesignFree() == 1) {
                                findViewById(R.id.llBtn).setVisibility(View.VISIBLE);
                                btnGo.setBackgroundResource(R.drawable.x_shape_gray_border_r20);
                                btnGo.setTextColor(Color.rgb(0x90,0x90,0x90));
                                btnGo.setText("此设计免费");
                                btnGo.setOnClickListener(null);
                            } else {
                                if (orderData != null && orderData.getPushStateId() == 1) {

                                    findViewById(R.id.llBtn).setVisibility(View.VISIBLE);
                                    if (orderData.getOrderStateId() == 2) {
                                        btnGo.setText("查看订单");

                                        btnGo.setOnClickListener(new View.OnClickListener() {
                                            @Override
                                            public void onClick(View view) {
                                                if (data != null) {
                                                    Intent intent = new Intent($this, RequireOrderDetailActivity.class);
                                                    intent.putExtra(KEY_PARAM, orderData);
                                                    startActivityForResult(intent, 1);
                                                }
                                            }
                                        });

                                    } else if (orderData.getOrderStateId() == 1) {
                                        btnGo.setText("继续未完成的订单");

                                        btnGo.setOnClickListener(new View.OnClickListener() {
                                            @Override
                                            public void onClick(View view) {
                                                if (data != null) {
                                                    Intent intent = new Intent($this, RequireOrderDetailActivity.class);
                                                    intent.putExtra(KEY_PARAM, orderData);
                                                    startActivityForResult(intent, 1);
                                                }
                                            }
                                        });

                                    } else {
                                        btnGo.setText("查看设计报价");

                                        btnGo.setOnClickListener(new View.OnClickListener() {
                                            @Override
                                            public void onClick(View view) {
                                                if (data != null) {
                                                    Intent intent = new Intent($this, OrderConfirmActivity.class);
                                                    intent.putExtra("requireData", data);
                                                    intent.putExtra(KEY_PARAM, orderData);
                                                    startActivityForResult(intent, 1);
                                                }
                                            }
                                        });
                                    }


                                }else if(data.getDesignStateId() == 1){
                                    btnGo.setBackgroundResource(R.drawable.x_shape_dark_gray_r3);
                                    String designName = null;
                                    if (data.getIsSubcontract() == 1){
                                        designName = data.getDesignRoleName();
                                    }else{
                                        designName = data.getDesignName();
                                    }

                                    if (designName == null){
                                        designName = "";
                                    }
                                    if (designName.length() > 4){
                                        designName = designName.substring(0,4);
                                    }
                                    btnGo.setText("设计师" + designName + "已接单，等待设计报价");
                                    btnGo.setTextSize(12);
                                } else {
                                    findViewById(R.id.llBtn).setVisibility(View.GONE);
                                }
                            }

                            if (data != null && !TextUtils.isEmpty(data.getDesignPhone())){
                                ivPhone.setVisibility(View.VISIBLE);
                            }else {
                                ivPhone.setVisibility(View.GONE);
                            }

                            if (data.getIsBidProject() == 1 && data.getIsBidProjectState() == 1){
                                findViewById(R.id.llBtn).setVisibility(View.VISIBLE);
                                btnGo.setText("查看投标");
                                ivPhone.setImageResource(R.drawable.i_phone_green_strike);
                                ivPhone.setBackgroundResource(R.drawable.x_shape_dark_gray_r3);
                                btnGo.setOnClickListener(new View.OnClickListener() {
                                    @Override
                                    public void onClick(View view) {
                                        startActivity(new Intent($this,BidListActivity.class).putExtra(KEY_ID,data.getId()));
                                    }
                                });
                            }
                        }
                    });

                }


            }
        });
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.fl_Left:
                finish();
                break;
            case R.id.ivPhone:
                if (data != null) {
                    String phone = data.getDesignPhone();
                    if (data.getIsSubcontract() == 1){
                        phone = data.getDesignRoleId();
                    }
                    Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + phone));
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(intent);
                }
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1 && requestCode == RESULT_OK) {
            //成功 关闭
            finish();
        }
    }
}
